<?php
include( "agent/onlinebet_header5.php" );
$sql = "select * from bet where bettype=29 and lotto_id=" . $lotto_id . " and rame in (0,9)";
if ($curruser['acl'] != 255)
{
    $userlist = GetUserList($curruser['id']);
    if ($userlist == "")
    {
        $ui->showErrorPage("<p>还没有建立会员！</p>");
        exit();
    }
    $sql .= " and user_id in (" . $userlist . ")";
}
$g_disk_code = array(1 => "a", 2 => "b", 3 => "c", 4 => "d");
$disk_code = "a";
if (0 < $disk)
{
    $sql .= " and disk=" . $disk;
    $disk_code = $g_disk_code[$disk];
}
$percent = 0;
$lostwin = array();
$totalamount = array();
$totalamount[29] = 0;
$totalcount = array();
$totalcount[29] = 0;
$ary = array();
$ary[29] = array();
$i = 0;
while ($i < 1200)
{
    $ary[29][$i] = array("num" => "", "amount" => 0);
    ++$i;
}
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$sql .= " order by unitmoney desc, bettype, numbers";
$rs = getdb($sql);
while (!$rs->eof)
{
    if ($split == 1)
    {
        if ($theacl == 2)
        {
            $percent = $rs->f['mpercent'];
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        else
        {
            if ($theacl == 3)
            {
                $percent = $rs->f['percent2'];
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            else
            {
                if ($theacl == 4)
                {
                    $percent = $rs->f['percent3'];
                    if (0 < $percent)
                    {
                        $percent /= 100;
                    }
                }
                else
                {
                    if ($theacl == 5)
                    {
                        $percent = $rs->f['percent4'];
                        $percent /= 100;
                    }
                    else
                    {
                        $percent = $rs->f['upercent'];
                        if (0 < $percent)
                        {
                            $percent /= 100;
                        }
                    }
                }
            }
        }
    }
    else
    {
        $percent = 1;
    }
    $bettype = $rs->f['bettype'];
    $numbers = $rs->f['numbers'];
    $num = explode(",", $rs->f['line_nums']);
    $numarry = Get_LM_Array($bettype, $num);
    $count = count($numarry);
    $tmp = $percent * $rs->f['unitmoney'];
    if ($betmin <= $tmp)
    {
        $i = 0;
        while ($i < $count)
        {
            $n = Scan_LM_Array($bettype, $numarry[$i], $ary, 1200);
            if ($n < 0)
            {
                $j = Empty_LM_Array($bettype, $ary, 1200);
                if (0 <= $j)
                {
                    $ary[$bettype][$j]['num'] = $numarry[$i];
                    $ary[$bettype][$j]['amount'] = $tmp;
                }
            }
            else
            {
                $ary[$bettype][$n]['amount'] += $tmp;
            }
            ++$i;
        }
    }
    ++$totalcount[$bettype];
    $totalamount[$bettype] += $tmp;
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    if ($theacl == 255)
    {
        $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and bettype=29 and rame in (0,9)";
    }
    else
    {
        $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype=29 and rame in (0,9)";
    }
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 255)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent255'] - 0;
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        else if ($theacl == 5)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent5'];
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent4'];
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back2'];
            $percent = $rs->f['percent3'];
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        if ($split != 1)
        {
            $percent = 1;
        }
        $bettype = $rs->f['bettype'];
        $numbers = $rs->f['numbers'];
        $num = explode(",", $rs->f['line_nums']);
        $numarry = Get_LM_Array($bettype, $num);
        $count = count($numarry);
        $tmp = $rs->f['unitmoney'] * $percent;
        if ($betmin <= $tmp)
        {
            $i = 0;
            while ($i < $count)
            {
                $n = Scan_LM_Array($bettype, $numarry[$i], $ary, 1200);
                if ($n < 0)
                {
                    $j = Empty_LM_Array($bettype, $ary, 1200);
                    if (0 <= $j)
                    {
                        $ary[$bettype][$j]['num'] = $numarry[$i];
                        $ary[$bettype][$j]['amount'] = $tmp;
                    }
                }
                else
                {
                    $ary[$bettype][$n]['amount'] += $tmp;
                }
                ++$i;
            }
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] += $tmp;
        $rs->movenext();
    }
}
$rs->close();
if ($theacl == 255)
{
    $sql = "select * from bet8 where lotto_id=" . $lotto_id . " and bettype=29 and rame in (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $bettype = $rs->f['bettype'];
        $p = explode("|", $rs->f['numbers']);
        $numbers = $p[3];
        $num = explode(",", $numbers);
        $numarry = Get_LM_Array($bettype, $num);
        $count = count($numarry);
        $tmp = $p[0];
        $i = 0;
        while ($i < $count)
        {
            $n = Scan_LM_Array($bettype, $numarry[$i], $ary, 1200);
            if ($n < 0)
            {
                $j = Empty_LM_Array($bettype, $ary, 1200);
                if (0 <= $j)
                {
                    $ary[$bettype][$j]['num'] = $numarry[$i];
                    $ary[$bettype][$j]['amount'] = $tmp;
                }
            }
            else
            {
                $ary[$bettype][$n]['amount'] -= $tmp;
            }
            ++$i;
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] -= $tmp;
        $rs->movenext();
    }
}
else
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype=29 and rame in (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $backs = 100;
        if ($theacl == 5)
        {
            $backs = $rs->f['back5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back3'];
        }
        else if ($theacl == 2)
        {
            $backs = $rs->f['back2'];
        }
        $bettype = $rs->f['bettype'];
        $numbers = $rs->f['numbers'];
        $num = explode(",", $rs->f['line_nums']);
        $numarry = Get_LM_Array($bettype, $num);
        $count = count($numarry);
        $tmp = $rs->f['unitmoney'];
        $i = 0;
        while ($i < $count)
        {
            $n = Scan_LM_Array($bettype, $numarry[$i], $ary, 1200);
            if ($n < 0)
            {
                $j = Empty_LM_Array($bettype, $ary, 1200);
                if (0 <= $j)
                {
                    $ary[$bettype][$j]['num'] = $numarry[$i];
                    $ary[$bettype][$j]['amount'] = $tmp;
                }
            }
            else
            {
                $ary[$bettype][$n]['amount'] -= $tmp;
            }
            ++$i;
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] -= $tmp;
        $rs->movenext();
    }
}
$rs->close();
$list = array("", "", "", "", "", "");
$tot = 0;
if (0 < $totalcount[29])
{
    $i = 0;
    while ($i < 1200)
    {
        $lostwin[29][$i] = 0 - $ary[29][$i]['amount'];
        ++$i;
    }
    asort($lostwin[29]);
    reset($lostwin[29]);
    $col = 1;
    $rn = 0;
    $ov = 0;
    foreach ($lostwin[29] as $key => $val)
    {
        if ($rn < 6)
        {
            $c = $ary[29][$key]['num'];
            $n = intval($ary[29][$key]['amount']);
            if ($c != "")
            {
                if ($col == 1)
                {
                    $list .= $rn;
                    $ov = 0;
                }
                $list .= $rn;
                $list .= $rn;
                $list .= $rn;
                ++$tot;
                ++$col;
                if (20 < $col)
                {
                    $list .= $rn;
                    $col = 1;
                    $ov = 1;
                    ++$rn;
                }
            }
        }
    }
    $list .= $rn;
}
$disk = $disk == 0 ? 1 : $disk;
$rs = getdb("select * from rate29 where lotto_id=" . $lotto_id . " and disk={$disk}");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rate_list = "";
$y = 0;
while ($y <= 4)
{
    $rate_list .= "<tr>";
    $x = 1;
    while ($x <= 10)
    {
        $num = d2($y * 10 + $x);
        if ($num == "50")
        {
            break;
        }
        $rate = $rs->f["r" . $num];
        $ball = "";
        if (array_search(intval($num), $g_red_po) !== false)
        {
            $ball = "background='image/red.gif'";
        }
        else if (array_search(intval($num), $g_blue_po) !== false)
        {
            $ball = "background='image/blue.gif'";
        }
        else
        {
            $ball = "background='image/green.gif'";
        }
        $rate_list .= "<td class='ball_td' align='middle' {$ball} bgColor='#c0d6eb' height='27' width='49'>" . "<b>{$num}</b></td><td bgColor='#e7ebef' nowrap> {$rate}</td>";
        ++$x;
    }
    $rate_list .= "</tr>";
    ++$y;
}
$tpl->set("rate_list", $rate_list);
$i = 0;
while ($i < 6)
{
    $tpl->set("list" . $i, $list[$i]);
    ++$i;
}
$tpl->set("totall", "显示排行前 <b>" . $tot . "</b> 笔下注资料");
$tpl->load("main", "onlinebet5_29.htm");
$tpl->show("main");
$ui->footer();